package lee.lxwei.boot.service;

import lee.lxwei.boot.domain.UserRegistEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;

/**
 * 注册事件发布者
 */
@Slf4j
@Service
public class UserRegistService implements ApplicationEventPublisherAware {

    // 注入事件发布者
    private ApplicationEventPublisher applicationEventPublisher;

    /**
     * 注入 事件发布类
     */
//    @Autowired
//    ApplicationEventPublisher eventPublisher;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    public boolean regist(String name) {
        log.info("用户注册 {} 并以事件处理后续任务  如发邮件 送优惠券", name);
        //发送事件
        applicationEventPublisher.publishEvent(new UserRegistEvent(this, name));
        return true;
    }
}
